<?php
// app/Model/User.php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {
    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Ein Benutzername wird benötigt'
            )
        ),
    		
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Ein Passwort wird benötigt'
            ),
        	'comp passwords' => array(
        		'rule' => 'compPasswords',
        		'message' => 'Die Passwörter stimmen nicht überein.'
        	)
        ),
    	
    	'repass' => array(
    		'required' => array(
    			'rule' => array('notEmpty'),
    			'message' => 'Ein Passwort wird benötigt'
    				)
    	),
    		
    	'email' => 'email',
    		
    	'gender' => array(
    		'valid' => array(
    			'rule' => array('inList', array('male', 'female'))
			)
		),

    	'birth' => array(
    		'valid' => array(
    		'rule' => array('notEmpty', 'date', 'monthNames' => false),
    		)
    	)
    
 //       'role' => array(
 //           'valid' => array(
 //               'rule' => array('inList', array('admin', 'standard')),
 //               'message' => 'Please enter a valid role',
 //               'allowEmpty' => false
 //           )
 //       )
    );
    
    public function compPasswords($data){
    	if ($data['password'] == $this->data['User']['repass']){
    		return true;
    	}
    	return false;
    }
    
    public function beforeSave($options = array()) {
    	if (isset($this->data[$this->alias]['password'])) {
    		$passwordHasher = new BlowfishPasswordHasher();
    		$this->data[$this->alias]['password'] = $passwordHasher->hash(
    				$this->data[$this->alias]['password']
    		);
    	}
    	return true;
    }
}
?>